package com.lwb.ztx.videos.wedget;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lwb.ztx.R;
import com.lwb.ztx.api.Api;
import com.lwb.ztx.bean.VideoBean;
import com.lwb.ztx.bean.VideoMainBean;
import com.lwb.ztx.bean.VideoTypeBean;
import com.lwb.ztx.utils.ImageLoaderUtils;
import com.lwb.ztx.utils.LogUtils;
import com.lwb.ztx.videos.presenter.VideoPresenter;
import com.lwb.ztx.videos.presenter.VideoPresenterImpl;
import com.lwb.ztx.videos.view.VideoView;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

public class VideoFragment extends Fragment implements VideoView, XRecyclerView.LoadingListener,
        VideoTypeAdapter.OnVideoTypeClickListener {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private Context mContext;
    @InjectView(R.id.videos_recycler_view)
    XRecyclerView recyclerView;
    VideoAdapter adapter;
    VideoTypeAdapter typeAdapter;
    RecyclerView header;

    private int page = 1;

    private VideoPresenter presenter;

    private View rootView;

    public VideoFragment() {
        // Required empty public constructor
    }

    public static VideoFragment newInstance(String param1, String param2) {
        VideoFragment fragment = new VideoFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        mContext = this.getActivity();
        presenter = new VideoPresenterImpl(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        if (rootView == null) {
            View view = inflater.inflate(R.layout.fragment_videos, container, false);
            ButterKnife.inject(this, view);
            header = (RecyclerView) inflater.inflate(R.layout.video_header_layout, container, false);
            recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
            adapter = new VideoAdapter(mContext);
            recyclerView.setAdapter(adapter);
            recyclerView.setLoadingListener(this);
            recyclerView.setLoadingMoreEnabled(true);
            initHeader(header);
            presenter.getVideos(Api.getVideoUrl(1),1);
            rootView = view;
        }
        ViewGroup parentView = (ViewGroup) rootView.getParent();
        if (parentView != null) {
            parentView.removeView(rootView);
        }
        return rootView;
    }

    private void initHeader(RecyclerView header) {
        LinearLayoutManager headerManager = new LinearLayoutManager(mContext);
        headerManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        header.setLayoutManager(headerManager);
        typeAdapter = new VideoTypeAdapter(this);
        header.setAdapter(typeAdapter);
        recyclerView.addHeaderView(header);
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }


    @Override
    public void setVideos(VideoMainBean bean) {
        LogUtils.i(bean.getBeans().toString());
        adapter.setBeans(bean.getBeans());
        adapter.notifyDataSetChanged();
        typeAdapter.setVideoTypeBeanList(bean.getTypes());
        typeAdapter.notifyDataSetChanged();
        recyclerView.refreshComplete();
    }

    @Override
    public void addMoreVideos(VideoMainBean bean) {
        adapter.addMoreBeans(bean.getBeans());
        adapter.notifyDataSetChanged();
        recyclerView.loadMoreComplete();
    }

    @Override
    public void onRefresh() {
        presenter.getVideos(Api.getVideoUrl(1),1);
        page = 1;
    }

    @Override
    public void onLoadMore() {
        page ++ ;
        presenter.getMoreVideos(Api.getVideoUrl(page),page);
    }

    @Override
    public void OnVideoTypeClick(VideoTypeBean videoTypeBean) {
        Intent intent = new Intent(this.getActivity(),VideoTypeDetailActivity.class);
        intent.putExtra("videoType",videoTypeBean);
        this.getActivity().startActivity(intent);
    }
}
